﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twipler.App.Parse.Code;
using Twipler.App.Render.Tree;
using Twipler.Data.Access;
using Twipler.Data.Entity.Theme;

namespace Twipler.App.Parse.Html.Consumers
{
	[Consumer(OpenTag = "include", Parameters = typeof(IncludeConsumer))]
	public class IncludeConsumer : TemplateConsumer
	{
		public class InlcudeParams : TemplateParameters
		{
			[HtmlArgument(TokenParser.TokenType.ItemName, true)]
			public string Code;

			[HtmlArgumentDynamic(TokenParser.TokenType.String, true)]
			public List<KeyValuePair<string, object>> Dynamic;
		}

		public IncludeConsumer()
		{
			Parameters = new InlcudeParams();
		}

		public override AbstractItem Process(Template template, TemplateConsumer parent)
		{
			ReadArguments(template);
			if (template.HasErrors)
				return ParseErrorItem.Create(template.Errors.First().Message);

			template.SkipWhiteSpace();
			if (template.Peek(Template.EndMarker, false) == false)
				return Error(template, "Missing template close tag (" + Template.EndMarker + ")");

			template.SkipOver(Template.EndMarker);

			// Get the include block
			string code = ((InlcudeParams)Parameters).Code;

			if (Current.Visitor.Design == null)
				return ParseErrorItem.Create("Current.Visitor does not have an associated design.");

			Block block = Factory.Block.GetByCode(Current.Visitor.Design, code).FirstOrDefault();
			if (block == null)
				return ParseErrorItem.Create(string.Format("Could not find block {0}.", code));

			string content = block.Content;
			
		//	if (block.UserCanModify())
		//		content = "<div style=\"position:relative;\"><a style=\"z-index:10; position:absolute; top:0px; left:0px; border: 1px solid #fff; padding: 4px 8px 4px 8px; background: #666; color: #fff;\" href=\"settings/theme/edithtml.aspx?thing=block&id=" + block.Id + " \">edit</a>" + block.Content + "</div>";
			
			// replace include sections
			if (((InlcudeParams)Parameters).Dynamic != null && ((InlcudeParams)Parameters).Dynamic.Count() > 0)
				content = ReplaceContent(content, ((InlcudeParams)Parameters).Dynamic);

			// return result of parsing the block
			return (new Template(content)).Parse();
		}

		private string ReplaceContent(string content, List<KeyValuePair<string, object>> replaceList)
		{
			StringBuilder work = new StringBuilder(content);

			foreach (KeyValuePair<string, object> replacement in replaceList)
				work.Replace("%" + replacement.Key + "%", replacement.Value.ToString());

			return work.ToString();
		}

	}
}